package com.demo.project_template.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.project_template.entity.SignIn;
import com.demo.project_template.exception.ErrorCodeException;
import com.demo.project_template.service.SignInService;
import com.demo.project_template.vo.response.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-08-20 18:03:50
 */
@Api(tags = "签到配置")
@RestController
@RequestMapping("/signIn")
public class SignInController {
    @Resource
    private SignInService signInService;

    @ApiOperation(value = "查询签到配置")
    @GetMapping(value = "/list")
    public R<IPage<SignIn>> list(@RequestParam Integer pageNo, @RequestParam Integer pageSize){
        return R.success(signInService.page(new Page<>(pageNo,pageSize),new LambdaQueryWrapper<SignIn>().orderByAsc(SignIn::getDaySort)));
    }

    @ApiOperation(value = "添加签到配置")
    @PostMapping(value = "/add")
    public R<?> add(@RequestBody SignIn signIn){
        if (signIn.getDaySort()<1 || signIn.getDaySort()>7){
            throw new ErrorCodeException("参数错误（天数）");
        }
        signInService.save(signIn);
        return R.success();
    }

    @ApiOperation(value = "修改签到配置")
    @PostMapping(value = "/update")
    public R<?> update(@RequestBody SignIn signIn){
        signInService.updateById(signIn);
        return R.success();
    }

    @ApiOperation(value = "删除签到配置")
    @DeleteMapping(value = "/id")
    public R<?> delete(@RequestParam Long id){
        signInService.removeById(id);
        return R.success();
    }

    @ApiOperation(value = "用户签到")
    @PostMapping(value = "/signIn")
    public R<?> signIn(@RequestParam Long userId){
        signInService.signIn(userId);
        return R.success();
    }
}
